package com.example.widget.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.widget.R;

import java.util.ArrayList;
import java.util.List;

public class TimeLineView extends View {
    private int width = 0;//控件宽度
    private int height = 0;//控件高度
    private Paint paint;//画笔
    //自定义属性传入数据
    private int lineColor;//横线颜色
    private int normalTxtColor;//默认文字颜色
    private int selTextColor;//完成进度文字颜色
    private int topTextColor;//顶部翻倍文字颜色
    private int selCircleColor;//翻倍以及完成外面环形颜色
    private int normalCircleColor;//默认环颜色
    private int bottomTxtSize = 9;//底部文字字号
    private int topTxtSize = 5;//顶部翻倍字号
    private int contentTxtSize = 14;//中间内容完成字号
    //全部待绘制数据集合
    private List<TimeLineEntity> lineEntities;
    private String[] contentStrs= {"5","10","10","20","25","25","30"};
    private int signNum = 2;
    private Bitmap selBitMap;
    public TimeLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);//创建画笔对象
        lineColor = Color.parseColor("#c1c1c1");
        normalTxtColor = Color.parseColor("#c1c1c1");
        selTextColor = Color.parseColor("#ff0000");
        topTextColor = Color.parseColor("#ffffff");
        selCircleColor = Color.parseColor("#ff0000");
        normalCircleColor = Color.parseColor("#ffffff");
        bottomTxtSize = ConvertUtils.sp2px(bottomTxtSize);
        topTxtSize = ConvertUtils.sp2px(topTxtSize);
        contentTxtSize = ConvertUtils.sp2px(contentTxtSize);
        lineEntities = new ArrayList<>();
        selBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.module_integral_now_checked);
    }

    private boolean flag = true;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        if (width >0&&height>0&&flag){
            flag = false;
            int itemWidth = width/(contentStrs.length*2+(contentStrs.length-1));
            int itemHeight = height/10;
            if (itemHeight > itemWidth) {
                for (int i = 0; i < contentStrs.length; i++) {
                    //配置第一个圆
                    if (i == 0) {
                        createWidthCircle(i, itemWidth, itemHeight);
                        createBottomTxt(i,itemWidth,itemHeight);
                        continue;
                    }
                    TimeLineEntity lineEntity = new TimeLineEntity();
                    lineEntity.type = 2;
                    lineEntity.color = lineColor;
                    lineEntity.startY = (float) (itemHeight * 4.5 - ConvertUtils.dp2px(0.5f));
                    lineEntity.stopY = (float) (itemHeight * 4.5 - ConvertUtils.dp2px(0.5f));
                    lineEntity.startX = itemWidth * (3 * i - 1);
                    lineEntity.stopX = lineEntity.startX + itemWidth;
                    lineEntities.add(lineEntity);
                    //配置圆
                    createWidthCircle(i, itemWidth, itemHeight);
                    createBottomTxt(i,itemWidth,itemHeight);
                }
            }else {
                for (int i = 0; i < contentStrs.length; i++){
                    if (i == 0){
                        createHeightCircle(i, itemWidth, itemHeight);
                        createBottomTxt(i,itemWidth,itemHeight);;
                        continue;
                    }
                    TimeLineEntity lineEntity = new TimeLineEntity();
                    lineEntity.type = 2;
                    lineEntity.color = lineColor;
                    lineEntity.startY = (float) (itemHeight * 4.5 - ConvertUtils.dp2px(0.5f));
                    lineEntity.stopY = (float) (itemHeight * 4.5 - ConvertUtils.dp2px(0.5f));
                    //2,5,8
                    lineEntity.startX = lastCX+itemHeight*5/2;//上一次得圆心点+半径
                    lineEntity.stopX = (3*i+1)*itemWidth-itemHeight*5/2;//本次圆心点-半径
                    lineEntities.add(lineEntity);
                    createHeightCircle(i, itemWidth, itemHeight);
                    createBottomTxt(i,itemWidth,itemHeight);
                }
            }
        }
    }

    //配置绘制圆信息
    private void createWidthCircle(int i, int itemWidth,int itemHeight){
        TimeLineEntity timeLineEntity = new TimeLineEntity();
        timeLineEntity.color = selCircleColor;//圆背景颜色
        timeLineEntity.radius = itemWidth;//圆半径
        timeLineEntity.cx = (3*i+1)*itemWidth;//计算圆形X轴值
        timeLineEntity.cy = (float) (itemHeight*4.5);//计算圆形Y轴值
        timeLineEntity.type = 0;
        lineEntities.add(timeLineEntity);
        //遮盖背景得圆
        TimeLineEntity whiteEntity = new TimeLineEntity();
        whiteEntity.type = 1;
        whiteEntity.cx = (3*i+1)*itemWidth;
        whiteEntity.cy = (float) (itemHeight*4.5);
        whiteEntity.color = normalCircleColor;
        lineEntities.add(whiteEntity);
        //根据选中状态绘制对勾或者文字
        TimeLineEntity txtEntity = new TimeLineEntity();
        txtEntity.type = 3;
        txtEntity.content = contentStrs[i];
        txtEntity.textX = (3*i+1)*itemWidth;
        txtEntity.textY = (float) (itemHeight*4.5);
        txtEntity.color = normalTxtColor;
        txtEntity.txtSize = contentTxtSize;
        lineEntities.add(txtEntity);
    }
    private float lastCX;
    private void createHeightCircle(int i, int itemWidth,int itemHeight){
        TimeLineEntity timeLineEntity = new TimeLineEntity();
        timeLineEntity.color = selCircleColor;//圆背景颜色
        timeLineEntity.radius = itemHeight*5/2;//圆半径
        timeLineEntity.cx = (3*i+1)*itemWidth;//计算圆形X轴值
        timeLineEntity.cy = (float) (itemHeight*4.5);//计算圆形Y轴值
        lastCX = timeLineEntity.cx;
        timeLineEntity.type = 0;
        lineEntities.add(timeLineEntity);
        if (i <= signNum){
            createSelBit(i,itemWidth,itemHeight);
        }else {
            TimeLineEntity whiteEntity = new TimeLineEntity();
            whiteEntity.type = 1;
            whiteEntity.cx = (3*i+1)*itemWidth;
            whiteEntity.cy = (float) (itemHeight*4.5);
            whiteEntity.radius = itemHeight*5/2-ConvertUtils.dp2px(1);
            whiteEntity.color = normalCircleColor;
            lineEntities.add(whiteEntity);
            TimeLineEntity txtEntity = new TimeLineEntity();
            txtEntity.type = 3;
            txtEntity.content = contentStrs[i];
            txtEntity.textX = (3*i+1)*itemWidth;
            txtEntity.textY = (float) (itemHeight*4.5+contentTxtSize/4);
            txtEntity.color = normalTxtColor;
            txtEntity.txtSize = contentTxtSize;
            lineEntities.add(txtEntity);
        }
    }

    //创建底部文字
    private void createBottomTxt(int i,int itemWidth,int itemHeight){
        TimeLineEntity entity = new TimeLineEntity();
        entity.content = (i==signNum)?"今天":""+i+"连签";
        entity.color = (i<=signNum)?selTextColor:normalTxtColor;
        entity.textX = (3*i+1)*itemWidth;
        entity.textY = getMeasuredHeight()-bottomTxtSize/4;
        entity.type = 3;
        entity.txtSize = bottomTxtSize;
        lineEntities.add(entity);
    }

    //创建要绘制得bitmap
    private void createSelBit(int i,int itemWidth,int itemHeight){
        TimeLineEntity entity = new TimeLineEntity();
        entity.type = 4;
        entity.selBitMap = selBitMap;
        entity.left = (float) (((3*i+1)*itemWidth)-(itemHeight*5/2));
        entity.top = (float) ((itemHeight*4.5)-(itemHeight*5/2));
        Matrix matrix = new Matrix();
        float bitWidth = selBitMap.getWidth();
        float width = itemHeight*5;
        float scall =width/bitWidth;
        Log.e("ZXY","bitWidth:"+bitWidth+"width:"+width+"scall:"+scall);
        matrix.preScale(scall,scall);  
        matrix.preTranslate(entity.left,entity.top);
        entity.matrix = matrix;
        lineEntities.add(entity);
    }

    //绘制圆方法
    private void timelineDrawCircle(Canvas canvas,TimeLineEntity entity){
        paint.reset();//重置画笔
        paint.setAntiAlias(true);
        paint.setColor(entity.color);
        canvas.drawCircle(entity.cx,entity.cy,entity.radius,paint);
    }
    //绘制线方法
    private void timelineDrawLine(Canvas canvas,TimeLineEntity entity){
        paint.reset();//重置画笔
        paint.setAntiAlias(true);
        paint.setColor(entity.color);
        canvas.drawLine(entity.startX,entity.startY,entity.stopX,entity.stopY,paint);
    }
    //绘制文字方法
    private void timelineDrawTxt(Canvas canvas,TimeLineEntity entity){
        paint.reset();//重置画笔
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(entity.txtSize);
        paint.setColor(entity.color);
        canvas.drawText(entity.content,entity.textX,entity.textY,paint);
    }

    private void drawContent(Canvas canvas){
        for (int i = 0;i < lineEntities.size();i ++){
            TimeLineEntity entity = lineEntities.get(i);
            if (entity.type == 0){
                timelineDrawCircle(canvas,entity);
            }else if (entity.type == 1){
                timelineDrawCircle(canvas,entity);
            }else if (entity.type == 2){
                timelineDrawLine(canvas,entity);
            }else if (entity.type == 3){
                timelineDrawTxt(canvas,entity);
            }else if (entity.type == 4){
                timelineDrawBitmap(canvas,entity);
            }
        }
    }

    //绘制对勾图片
    private void timelineDrawBitmap(Canvas canvas, TimeLineEntity entity){
        paint.reset();
        paint.setAntiAlias(true);
        canvas.drawBitmap(entity.selBitMap,entity.matrix,paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        drawContent(canvas);
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            float x = event.getX();
            float y = event.getY();

        }
        return super.onTouchEvent(event);
    }
}
